import java.util.concurrent.CopyOnWriteArraySet;
/**
 * @author GAX
 * @date 2025-09-07
 */

public class BlacklistChecker {
    private final CopyOnWriteArraySet<String> blacklist =
            new CopyOnWriteArraySet<>();

    // 初始化黑名单
    public BlacklistChecker() {
        blacklist.add("spam@example.com");
        blacklist.add("malicious@site.com");
        blacklist.add("test@test.com");
    }

    // 添加黑名单条目（写操作少）
    public void addToBlacklist(String entry) {
        blacklist.add(entry);
        System.out.println("Added to blacklist: " + entry);
    }

    // 检查是否在黑名单中（读操作频繁）
    public boolean isBlacklisted(String entry) {
        boolean result = blacklist.contains(entry);
        System.out.println("Checking '" + entry + "': " + result);
        return result;
    }

    public static void main(String[] args) {
        BlacklistChecker checker = new BlacklistChecker();
        // 多个线程同时检查
        new Thread(() -> checker.isBlacklisted("user@example.com")).start();
        new Thread(() -> checker.isBlacklisted("spam@example.com")).start();
        new Thread(() -> checker.addToBlacklist("new@spam.com")).start();
        new Thread(() -> checker.isBlacklisted("new@spam.com")).start();
    }
}
